﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blizzard.Contracts;
using System.Reflection;

namespace Blizzard.Mapper
{
   public class AttrMapper : IMapper
    {

        public string GetTableName(Type type)
        {
            object res = null;
            object[] attributes = type.GetCustomAttributes(false);


            if (attributes.Length > 0)
                res = ((MapperAttribute)attributes[0]).Table;

            return res == null ? "" : res.ToString(); ;
        }
        public string GetIdentityFieldName(Type type)
        {
            object res = null;

            foreach (PropertyInfo pi in type.GetProperties())
            {

                if (IsIdentityColumn(pi))
                {
                    res = GetFieldName(pi);
                }

            }


            return res == null ? "" : res.ToString();
        }
        public  bool IsIdentityColumn(PropertyInfo pi)
        {

            object res = null;
            object[] attributes = pi.GetCustomAttributes(false);

            if (attributes.Length > 0)
                res = ((MapperAttribute)attributes[0]).IsIdentityColumn;

            return res == null ? false : (bool)res;

        }
        public string GetFieldName(PropertyInfo pi)
        {
            object res = null;
            object[] attributes = pi.GetCustomAttributes(false);

            if (attributes.Length > 0)
                res = ((MapperAttribute)attributes[0]).Field;

            return res == null ? "" : res.ToString();
        }
        public bool IsPartOfComposition(PropertyInfo pi)
        {

            object res = null;
            object[] attributes = pi.GetCustomAttributes(false);

            if (attributes.Length > 0)
                res = ((MapperAttribute)attributes[0]).Composition;

            return res == null ? false : (bool)res;

        }
        public string GetJoinTableName(PropertyInfo pi)
        {
            object res = null;
            object[] attributes = pi.GetCustomAttributes(false);

            if (attributes.Length > 0)
                res = ((MapperAttribute)attributes[0]).JoinTable;

            return res == null ? "" : res.ToString();
        }
        public string GetMyIdFieldName(PropertyInfo pi)
        {
            object res = null;
            object[] attributes = pi.GetCustomAttributes(false);

            if (attributes.Length > 0)
                res = ((MapperAttribute)attributes[0]).MyIdField;

            return res == null ? "" : res.ToString();
        }

    }
}
